package cn.jiyun.user.controller;

import cn.jiyun.user.pojo.Users;
import cn.jiyun.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     *  页面跳转  add  update  login
     */
    @GetMapping("/{page}")
    public String toPage(@PathVariable("page") String page){
        return page;
    }

    /**
     * 添加用户
     */
    @RequestMapping("/addUser")
    public String addUser(Users users) {
        userService.saveUser(users);
        return "ok";
    }

    /**
     * 查询所有用户
     */
    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Users> list = userService.findAll();
        model.addAttribute("list",list);
        return "list";
    }

    /**
     * 登录方法
     */
    @PostMapping("/login")
    public String login(String nam,String pwd,Model model){
        Users users = userService.login(nam,pwd);
        model.addAttribute("users",users);
        return "ok";
    }
}
